package com.adou.d1;

import org.junit.Test;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;

/**
 * 执行DQL
 */
public class JDBCDemo4 {
    @Test
    public void testResultSet() throws Exception {
        // 1.注册驱动
        Class.forName("com.mysql.jdbc.Driver");

        String url = "jdbc:mysql://127.0.0.1/db1?useSSL=false";
        String username = "root";
        String password = "root";
        // 2.获取连接
        Connection conn = DriverManager.getConnection(url, username, password);

        // 3.sql
        String sql = "select * from account1";

        // 4.获取执行对象
        Statement stmt = conn.createStatement();

        // 5.执行sql
        ResultSet rs = stmt.executeQuery(sql);

        // 6.处理数据
        while(rs.next()) {
            // 获取数据
            int id = rs.getInt(1);
            String name = rs.getString(2);
            Double money = rs.getDouble(3);
            System.out.println(id + " - " + name + " - " + money);
        }

        // 7.释放资源
        rs.close();
        stmt.close();
        conn.close();
    }
}
